# -*- encoding: utf-8 -*-
"""
@File    :   get_envinfo.py
@Desc    :   读取环境配置文件内容
"""

import configparser
import pathlib

from conf.settings import CONF_DIR


class ReadEnv(object):
    def __init__(self):
        """设置ENV配置文件路径"""
        self.file_path = pathlib.PurePath().joinpath(
            CONF_DIR,
            'env_info.ini',
        )  # 配置文件路径
        self.config = configparser.ConfigParser()
        self.contents = self.load_ini()  # 配置文件内容

    def load_ini(self):
        """
        读取配置文件
        -------
        :return contents: 返回文件内容
        """

        contents = self.config.read(self.file_path, encoding='utf-8')
        return contents

    def get_value(self, node: str, key: str):
        """
        获取配置文件节的信息
        ---------
        :param node: 节点名称
        :param key: 匹配键名称
        -------
        :return value: 返回值信息
        """

        sections = self.config.sections()
        if node in sections:
            value = self.config.get(
                node,
                key,
                fallback=f'No such key "{key}"',
            )
        else:
            value = None
        return value


env = ReadEnv()

if __name__ == '__main__':
    cf = ReadEnv()
    cont = cf.get_value('BHG', 'HOST')
    print(cont)
